package software.hust.com.hust_last.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import software.hust.com.hust_last.MyApplication;
import software.hust.com.hust_last.R;
import software.hust.com.hust_last.bean.MyPackage;
import software.hust.com.hust_last.presenter.AddTreasurePresenter;
import software.hust.com.hust_last.util.ToastUtil;


/**
 * @des 新建宝藏
 */
public class AddTreasureActivity extends Activity implements
        View.OnClickListener, RadioGroup.OnCheckedChangeListener,
        android.widget.CompoundButton.OnCheckedChangeListener {

    public static final String TAG = "AddTreasureActivity";

    public static final int GET_FRIEND_NAME_SUCESS = 4;
    public static final int GET_VIRTUALTOYS_SUCESS = 3;

    private MyPackage pointProp = null;

    private ImageView show_virtual_toy_img;
    private TextView show_virtual_toy_tv;
    private RadioGroup treasure_money_style_rg, treasure_variety_rg;
    private Button add_treasure_btn;
    private EditText show_virtual_num_tv, show_people_num_tv, get_money_num_et,
            treasure_address_et, set_radius_num_tv;
    private LinearLayout show_money_style_layout, show_virtual_toy_layout;

    private int rediusNumMoney = 100;
    // 设置红包类分配人数
    private int peopleNumMoney = 1;
    // 设置宝贝类分配人数
    private int peopleNumVirtual = 1;
    // 设置红包数量
    private int moneyNumMoney = 1;
    // 保存当前埋藏宝藏的类型(默认为宝贝宝藏)
    private String reward_type = "point_reward";
    // 保存当前红包的分配策略(默认为平均分配)
    private String assign_type = "average";
    // 保存当前的宝藏类型(默认为公共宝藏)
    private String treasure_type = "public";
    // 临时保存宝藏类型
    private String temporary_treasure_type;
    // 保存得到的口令码
    private String treasure_password = "";
    // 选中的好友ID
    private StringBuilder selectedID = new StringBuilder();
    //选中的道具具有的最大数量
    private int max_prop_num = 0;
    private StringBuilder userStr = new StringBuilder();
    private AddTreasurePresenter addPresenter;

    private String path_latitude = "", path_longitude = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_treasure);

        initViews();
        initData();
        initLinstener();
    }

    /**
     * @des 初始化UI
     */
    private void initViews() {

        treasure_address_et = (EditText) findViewById(R.id.treasure_address_et);
        show_people_num_tv = (EditText) findViewById(R.id.show_people_num_tv);
        show_virtual_toy_tv = (TextView) findViewById(R.id.show_virtual_toy_tv);
        show_virtual_num_tv = (EditText) findViewById(R.id.show_virtual_num_tv);
        treasure_money_style_rg = (RadioGroup) findViewById(R.id.treasure_money_style_rg);
        treasure_variety_rg = (RadioGroup) findViewById(R.id.treasure_variety_rg);
        get_money_num_et = (EditText) findViewById(R.id.get_money_num_et);
        set_radius_num_tv = (EditText) findViewById(R.id.set_radius_num_tv);
        add_treasure_btn = (Button) findViewById(R.id.add_treasure_btn);
        show_virtual_toy_img = (ImageView) findViewById(R.id.show_virtual_toy_img);
        show_money_style_layout = (LinearLayout) findViewById(R.id.show_money_style_layout);
        show_virtual_toy_layout = (LinearLayout) findViewById(R.id.show_virtual_toy_layout);
    }


    /**
     * @des 初始化数据
     */
    private void initData() {
        addPresenter = new AddTreasurePresenter(this);

        show_money_style_layout.setVisibility(View.GONE);
        show_virtual_toy_layout.setVisibility(View.GONE);
        treasure_address_et.setText("我的公共宝藏");
        // 宝贝宝藏，显示宝贝UI
        show_virtual_toy_layout.setVisibility(View.VISIBLE);
    }

    /**
     * @des 初始化监听器
     */
    private void initLinstener() {
        // 宝藏宝贝选择
        show_virtual_toy_img.setOnClickListener(this);
        // 新建宝藏
        add_treasure_btn.setOnClickListener(this);
        // 宝藏种类监听
        treasure_money_style_rg.setOnCheckedChangeListener(this);
        treasure_variety_rg.setOnCheckedChangeListener(this);

        get_money_num_et.addTextChangedListener(new moneyWatcher());
        show_people_num_tv.addTextChangedListener(new peopleWatcher());
        show_virtual_num_tv.addTextChangedListener(new virtualWatchaer());
        set_radius_num_tv.addTextChangedListener(new radiusWatcher());
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            // 宝藏宝贝选择
            case R.id.show_virtual_toy_img:
                Intent intent1 = new Intent();
                intent1.setClass(getApplicationContext(), MyPackageAct.class);
                intent1.putExtra("type", 1);
                startActivityForResult(intent1, GET_FRIEND_NAME_SUCESS);

                break;
            // 新建宝藏
            case R.id.add_treasure_btn:
                // 公共宝藏模式
                if (checkPublicData()) {
                    if ("password".equals(treasure_type)) {
                        // 口令类型
                        if (checkDataForOrder()) {
                            addTreasure();
                        }
                    } else {
                        // 普通公共宝藏
                        addTreasure();
                    }
                }
                break;
        }
    }

    /**
     * @return
     * @des 口令宝藏检查项
     */
    private boolean checkDataForOrder() {
        if (!"".equals(treasure_password)) {
            return true;
        }
        ToastUtil.show(getApplicationContext(), "口令码检验失败，请重新获取口令码");
        return false;
    }

    /**
     * @return
     * @des 私有宝藏检查项
     */
    private boolean checkDataForPrivate() {
        // 检查宝藏分享人数
        if ("".equals(selectedID)) {
            ToastUtil.show(getApplicationContext(), "邀请至少一个好友才可以哦");
            return false;
        }
        return true;
    }

    /**
     * @return
     * @des 检查公共宝藏类型下的上传数据
     */
    private boolean checkPublicData() {

        if (!"money_reward".equals(reward_type)) {
            // 在宝贝宝藏类型下检查是否选择了宝贝
            if ("未选择".equals(show_virtual_toy_tv.getText().toString())) {
                ToastUtil.show(getApplicationContext(), "请选择要放入的宝贝");
                return false;
            }
        }
        if ("".equals(treasure_address_et.getText().toString())
                || "".equals(treasure_address_et.getText().toString().trim())) {
            ToastUtil.show(getApplicationContext(), "给你的宝藏起个名字吧");
            return false;
        }
        if (Integer.valueOf(get_money_num_et.getText().toString()) > 200) {
            ToastUtil.show(getApplicationContext(), "红包最多只能放置200元");
            return false;
        }
        return true;
    }

    /**
     * @des 新建宝藏
     */
    private void addTreasure() {

        String propId = "";
        String reward_count = "";
        String selectedID = "";

        if (pointProp != null) {
            // 说明是宝贝宝藏
            propId = pointProp.getProp_id() + "";
        }
        if ("point_reward".equals(reward_type)) {
            // 宝贝类型
            reward_count = show_virtual_num_tv.getText().toString();
        } else {
            // 红包类型
            reward_count = show_people_num_tv.getText().toString();
        }
        if (selectedID == null || selectedID.equals("")) {
            selectedID = "";
        } else {
            selectedID = selectedID.toString().substring(0, selectedID.toString().length() - 1);
        }
//        addPresenter.addTreasure(treasure_type, reward_type, propId, show_virtual_num_tv.getText().toString(), assign_type, reward_count, MyApplication.mApplication.CreateTreasureLatLng.latitude
//                        + "", MyApplication.mApplication.CreateTreasureLatLng.longitude
//                        + "", "", "", selectedID, treasure_password,
//                set_word_to_others_et.getText().toString(), get_money_num_et
//                        .getText().toString(), treasure_address_et.getText()
//                        .toString(), path_latitude, path_longitude);

        addPresenter.addTreasure(MyApplication.mApplication.CreateTreasureLatLng.longitude + "", MyApplication.mApplication.CreateTreasureLatLng.latitude + "",
                "", "", treasure_address_et.getText().toString(), propId, "", treasure_type, treasure_password, "", "", reward_type, get_money_num_et
                        .getText().toString(), assign_type, reward_count, rediusNumMoney + "");
    }

    public void AddTreasureSuccess() {

        ToastUtil.show(getApplicationContext(), "藏宝成功");
        Intent intent = new Intent();
        intent.putExtra("treasure_type",
                treasure_type);
        intent.putExtra("treasure_password",
                treasure_password);
        intent.setClass(
                getApplicationContext(),
                MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == GET_FRIEND_NAME_SUCESS) {
            switch (resultCode) {
                case GET_VIRTUALTOYS_SUCESS:
                    // 得到用户选择的宝贝
                    pointProp = (MyPackage) data.getSerializableExtra("MyPackage");
                    max_prop_num = Integer.valueOf(pointProp.getProp_num());
                    show_virtual_toy_tv.setText(pointProp.getProp_name());
                    break;
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        switch (arg0.getCheckedRadioButtonId()) {

            case R.id.money_treasure_rb:

                if (checkPayInfo()) {
                    // 红包宝藏，显示红包UI
                    show_money_style_layout.setVisibility(View.VISIBLE);
                    // 隐藏宝贝模式
                    show_virtual_toy_layout.setVisibility(View.GONE);
                    reward_type = "money_reward";
                } else {
                    // 设置选中焦点返回到宝贝宝藏上
                    ((RadioButton) treasure_variety_rg.getChildAt(1))
                            .setChecked(true);
                    ToastUtil.show(getApplicationContext(), "您还未绑定支付信息，请到“我的个人信息”中进行绑定");
                }

                break;
            case R.id.virtual_treasure_rb:
                // 宝贝宝藏，显示宝贝UI
                show_virtual_toy_layout.setVisibility(View.VISIBLE);
                // 隐藏红包模式
                show_money_style_layout.setVisibility(View.GONE);
                reward_type = "point_reward";
                break;
            case R.id.money_average_rb:
                // 平均分配
                assign_type = "average";
                break;
            case R.id.virtual_random_rb:
                // 随机分配
                assign_type = "random";
                break;
        }
    }

    /**
     * @return
     * @des 检查当前用户是否绑定了支付信息
     */
    private boolean checkPayInfo() {
        if ("".equals(MyApplication.mApplication.loginUserInfo.real_name) || MyApplication.mApplication.loginUserInfo.real_name == null
                || "".equals(MyApplication.mApplication.loginUserInfo.alipay_account)
                || MyApplication.mApplication.loginUserInfo.alipay_account == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (path_latitude == null) {
            //设置宝藏类型为公共宝藏
            treasure_type = "public";
        }
    }

    private class moneyWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if ("".equals(s.toString())) {
                moneyNumMoney = 0;
            } else {
                moneyNumMoney = Integer.valueOf(s.toString());
            }
            if (moneyNumMoney > 200) {
                ToastUtil.show(getApplicationContext(), "最多放入200元");
                moneyNumMoney = 200;
                get_money_num_et.setText(moneyNumMoney + "");
            } else if (moneyNumMoney < 1) {
                ToastUtil.show(getApplicationContext(), "最少放入1元");
                moneyNumMoney = 1;
                get_money_num_et.setText(moneyNumMoney + "");
            }
        }
    }

    //
    private class peopleWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if ("".equals(s.toString())) {
                peopleNumMoney = 0;
            } else {
                peopleNumMoney = Integer.valueOf(s.toString());
            }
            if (peopleNumMoney > 20) {
                ToastUtil.show(getApplicationContext(), "最多分配20人");
                peopleNumMoney = 20;
                show_people_num_tv.setText(peopleNumMoney + "");
            } else if (peopleNumMoney < 1) {
                ToastUtil.show(getApplicationContext(), "最少分配1人");
                peopleNumMoney = 1;
                show_people_num_tv.setText(peopleNumMoney + "");
            }
        }
    }

    private class virtualWatchaer implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            if ("".equals(s.toString())) {
                peopleNumVirtual = 0;
            } else {
                peopleNumVirtual = Integer.valueOf(s.toString());
            }
            if (max_prop_num != 0) {
                if (peopleNumVirtual > max_prop_num) {
                    ToastUtil.show(getApplicationContext(), "宝藏最多" + max_prop_num + "个");
                    peopleNumVirtual = max_prop_num;
                    show_virtual_num_tv.setText(peopleNumVirtual + "");
                } else if (peopleNumVirtual < 1) {
                    ToastUtil.show(getApplicationContext(), "最少分配1人");
                    peopleNumVirtual = 1;
                    show_virtual_num_tv.setText(peopleNumVirtual + "");
                }
                return;
            }
            if (peopleNumVirtual > 20) {
                ToastUtil.show(getApplicationContext(), "最多分配20人");
                peopleNumVirtual = 20;
                show_virtual_num_tv.setText(peopleNumVirtual + "");
            } else if (peopleNumVirtual < 1) {
                ToastUtil.show(getApplicationContext(), "最少分配1人");
                peopleNumVirtual = 1;
                show_virtual_num_tv.setText(peopleNumVirtual + "");
            }
        }
    }

    private class radiusWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            if ("".equals(s.toString())) {
                rediusNumMoney = 100;
            } else {
                rediusNumMoney = Integer.valueOf(s.toString());
            }
        }
    }
}
